//
//  LZResetPasswordViewController.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/25.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZResetPasswordViewController.h"
#import "LZLoginViewController.h"
#import "LZLoginTextFieldView.h"
#import "LZRetrieveTitleView.h"

#import "UIViewController+NavigationItemSetting.h"
#import "UIView+Rotation.h"

#import "Masonry.h"

#import "LZTextField.h"
#import "LZRemindView.h"
#import "LZUserInfoPresenter.h"

@interface LZResetPasswordViewController ()

@property (nonatomic, strong) UIImageView * bgImageView;
@property (nonatomic, strong) UIButton * submitButton;
@property (nonatomic, strong) LZLoginTextFieldView * inputPasswordView;
@property (nonatomic, strong) LZRetrieveTitleView * titleView;

@property (strong, nonatomic) UIButton *nav_left_back;
@property (assign, nonatomic) CGFloat lzAlpha;

@property (nonatomic, strong) LZUserInfoPresenter * userInfoPresenter;

@end

@implementation LZResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"重置密码页面";
    
    self.lzAlpha = 0;
    
    self.userInfoPresenter = [[LZUserInfoPresenter alloc] init];
    
    
    [self configViews];
    
    [self configAutoLayout];
    
    [self configNavigationBar];
    
    
    self.inputPasswordView.buttonAction_TF_Block = ^(LZLOGIN_TF_BUTTON_TAG buttonTag) {
        if (buttonTag == LZLOGIN_TF_BUTTON_TAG_SHOWPASSWORD) {
            DLog(@"显示密码");
        }
    };
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)fd_prefersNavigationBarHidden {
    return YES;
}
#pragma mark - Private method

- (void)configNavigationBar {
    
    self.nav_left_back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nav_left_back.frame = CGRectMake(0, 0, 60, 40);
    [self.nav_left_back addTarget:self action:@selector(navbarBackButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image_back = [UIImage imageNamed:@"lz_back"];
    [self.nav_left_back setImage:image_back forState:UIControlStateNormal];
    self.nav_left_back.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.nav_left_back.bounds)-image_back.size.width);
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        [self.nav_left_back setLanguageRotaion];
    }
    
    [self hiddenBarNavigationItemSetting:@[self.nav_left_back] type:NAVIGATIONITEMSETTING_LEFT];
    [self lz_setCustomBarAlpha:self.lzAlpha];
    
}

- (void)configViews {
    
    [self.view addSubview:self.bgImageView];
    [self.view addSubview:self.titleView];
    [self.view addSubview:self.inputPasswordView];
}

- (void)configAutoLayout {
    
    GET_SCREEN_SCALE(scale);
    
    [self.titleView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(-SCREEN_HEIGHT/4.0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(160*scale);
    }];
    
    [self.inputPasswordView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.view.centerY);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(55.0*scale);
    }];
    
    [self.view addSubview:self.submitButton];
    
    [self.submitButton makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(55.0);
    }];
}

#pragma mark - event

- (void)navbarBackButtonClickAction:(UIButton*)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)buttonAction:(UIButton *)sender {
    
    WS(weakSelf);
    if (self.inputPasswordView.passwordTextField.text.length <= 0) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"error_image"]
                         andContent:LZLocalizedString(@"enter_password", @"")];
        return;
    }
    else if (self.inputPasswordView.passwordTextField.text.length > 0 &&
               self.inputPasswordView.passwordTextField.text.length < 6) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"error_image"]
                         andContent:LZLocalizedString(@"enter_password_6_chart", @"")];
        return;
    }
    
    [self startLoadingViewForView:self.view loadingViewUserInteractionEnabled:YES];
    
    [self.userInfoPresenter userInitPasswordWithMoblieNum:self.userName
                                              andPassword:self.inputPasswordView.passwordTextField.text
                                                onSuccess:^(id data) {
                                                    
                                                    [weakSelf stopLoadingViewForView:weakSelf.view];
                                                    //跳转，登陆页面
                                                    for (UIViewController *vc in weakSelf.navigationController.viewControllers) {
                                                        if ([vc isKindOfClass:[LZLoginViewController class]]) {
                                                            [self.navigationController popToViewController:vc animated:YES];
                                                            return;
                                                        }
                                                    }
                                                } onFail:^(NSString *fail) {
                                                    DLog(@"%@", fail);
                                                    [weakSelf stopLoadingViewForView:weakSelf.view];
                                                    
                                                    [LZRemindView initWithImage:[UIImage imageNamed:@"error_image"]
                                                                     andContent:LZLocalizedString(@"reset_password_fail", @"")];
                                                }];
}

#pragma mark - Setter/Getter

- (UIImageView *)bgImageView {
    
    if (_bgImageView == nil) {
        _bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _bgImageView.image = [UIImage imageNamed:@"lz_bg"];
    }
    
    return _bgImageView;
}

- (UIButton *)submitButton {
    
    if (_submitButton == nil) {
        _submitButton = [[UIButton alloc] init];
        _submitButton.backgroundColor = UIColorFromRGB(0xf2871c, 1);
        GET_SCREEN_SCALE(scale);
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:17.0*scale];
        [_submitButton setTitle:LZLocalizedString(@"review_submit", nil) forState:UIControlStateNormal];
        [_submitButton setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
        [_submitButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _submitButton;
}

- (LZLoginTextFieldView *)inputPasswordView {
    
    if (_inputPasswordView == nil) {
        _inputPasswordView = [[LZLoginTextFieldView alloc] initWithPage:LZLOGIN_TF_TYPE_PAGE_PASSWORD];
    }
    
    return _inputPasswordView;
}

- (LZRetrieveTitleView *)titleView {
    
    if (_titleView == nil) {
        _titleView = [[LZRetrieveTitleView alloc] initWithPage:LZRETRIEVE_TITLE_PAGE_RESET];
        _titleView.subTitleText = [LZLocalizedString(@"reset_password_remind", nil) copy];
    }
    
    return _titleView;
}

@end
